package uiLayer;
import java.util.Scanner;
import controlLayer.*;
import modelLayer.*;
import java.util.ArrayList;

public class RentUI {
    private CustomerUI custUI;
    private EmployeeUI empUI;
    private MachineUI mcnUI;
    private CustomerCtr custCtr;
    private EmployeeCtr empCtr;
    private RentCtr rentCtr;
    private MachineCtr mcnCtr;
    private Customer cust;
    private Employee emp;
    private Machine mcn;
    private Item it;
    int ccpr;
    int ecpr;
    String period;
    String rentDate;
    private double total;
    
    public RentUI() {
        custUI = new CustomerUI();
        empUI = new EmployeeUI();
        mcnUI = new MachineUI();
        custCtr = new CustomerCtr();
        rentCtr = new RentCtr();
        mcnCtr = new MachineCtr();
        empCtr = new EmployeeCtr();
    }
    
    public void rentMenu() {
        boolean stop = false;
        
        while(!stop) {
            int choice = writeRentMenu();
            
            switch(choice) {
            case 1 : createRent();
                    stop = true;
                break;
            case 2 : findRent();
                break;
            case 3 : returnRent();
                break;
            case 0 : stop = true;
                break;
            default : System.out.println("Invalid command");
                      MainMenuUI.pause();
                break;
            }
        }
    } 
    
    private int writeRentMenu() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f*** Rent Menu ***");
        System.out.println("1. Create rent ");
        System.out.println("2. Find rent ");
        System.out.println("3. Remove rent ");
        System.out.println("0. Return to main menu ");
        System.out.println("\n Type your choice");
        
        int choice = keyboard.nextInt();
        
        return choice;
    }
    
    private void createRent() {
        ccpr = custUI.inputCprNo();
        cust = (custCtr.findCustomer(ccpr));
        if(cust != null) {
            ecpr = empUI.inputCprNo();
            emp = (empCtr.findEmployee(ecpr));
            if(emp != null) {
                int barcode = mcnUI.inputBarcode();
                mcn = mcnCtr.findMachine(barcode);
                if(mcn != null) {
                    it = assignItem();
                    if(it == null || mcn.getItemsNo() <= 0) {
                        System.out.println();
                        System.out.println("There are no available items of the selected machine.");
                    }
                    else {
                        String rentDate = inputRentDate();
                        String period = inputPeriod();
                        mcn.setItemsNo(mcn.getItemsNo()-1);
                        it.setAvailability(false);
                        total = Integer.parseInt(period) * mcn.getPricePerDay();
                        rentCtr.createRent(ccpr, ecpr, barcode, period, rentDate, total);
                        System.out.println();
                    }
                }
                 else {
                       System.out.println("No machine was found with the specified serial number"); 
                 }
            }
            else {
                System.out.println("No employee was found with the specified id");
            }
        }
        else {
            System.out.println("No customer was found with the specified id");
        }
        MainMenuUI.pause();
    }
    
    private void findRent() {
        int id = inputId();
        Rent rent = (rentCtr.findRent(id));
        if (rent != null) {
            System.out.println();
            showDate(rent.getRentDate());
            showStatus(rent.getStatus());
            System.out.println();
            Customer customer = rent.getCustomer();
            System.out.println("Customer name : " + customer.getName());
            System.out.println("Customer CPR numer : " + customer.getCpr());
            System.out.println();
            Employee employee = rent.getEmployee();
            System.out.println("Employee name : " + employee.getName());
            System.out.println("Eployee CPR : " + employee.getCpr());
            System.out.println();
            System.out.println("Name\t\tbarcode\t\tprice per day\t\tnumber of days");
            Machine machine = rent.getMachine();
            System.out.println(machine.getName() + "\t\t"+ machine.getBarcode() + "\t\t\t" + machine.getPricePerDay() + "\t\t" + rent.getPeriod());
            System.out.println();
            showTotalPrice(rent.getTotal());
            System.out.println(" <<Item lend>> ");
            System.out.println("Item serial number : " + it.getSerialNumber());
        }
        else {
            System.out.println("No rent was found with the specified id.");
        }
        System.out.println();
        MainMenuUI.pause();
    }
    
    private void returnRent() {
        Scanner keyboard = new Scanner(System.in);
        int id = inputId();
        Rent rent = (rentCtr.findRent(id));
        boolean stop = false;
        if (rent != null) {
            System.out.println();
            showDate(rent.getRentDate());
            showPeriod(rent.getPeriod());
            showStatus(rent.getStatus());
            
            System.out.println();
            System.out.println("1. Return rent");
            System.out.println("0. Return to previous menu");
            int choice = keyboard.nextInt();
            
            while(!stop) {
                stop = true;
                
                switch(choice) {
                    case 1 : it.setAvailability(true);
                             rent.setStatus("returned");
                             System.out.println();
                             System.out.println("The rent was returned");
                             System.out.println();
                             mcn.setItemsNo(mcn.getItemsNo()+1);
                             MainMenuUI.pause();
                        break;
                    case 0 : stop = true;
                        break;
                }
            }
        }
        else {
            System.out.println("No rent was found with the specified id.");
        }
    }
    
    private int inputId() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter an id (numeric value) for the rent : ");
        int rid = keyboard.nextInt();
        return rid;
    }
    
    private String inputRentDate() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the rent starting date (dd/mm/yyyy) : ");
        String date = keyboard.nextLine();
        return rentDate;
    }
    
    private String inputPeriod() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the rent period (number of days): ");
        String period = keyboard.nextLine();
        return period;
    }
    
    public int inputQuantity() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the quantity : ");
        int quantity = keyboard.nextInt();
        return quantity;
    }
    
    public void showDate(String date) {
        System.out.println("Rent starting date : " + date);
    }
    
    public void showTotalPrice(double totalPrice) { 
        System.out.println("Total price : " + totalPrice);
    }
    
    public void showStatus(String status) {
        System.out.println("Status : " + status);
    }
    
    public void showPeriod(String period) {
        System.out.println("Rent period : " + period);
    }
    
    private Item assignItem() {
        boolean found = false;
        int i = 0;
        Item it = null;
        while(i < mcn.items.size() && !found) {
            it = mcn.items.get(i);
            if(it.getAvailability()) {
                found = true;
            }
            else {
                i++;
            }
        }
        return it;
    }
         
}
